package com.neborosoft.FlyingDog.logic.database.content.identified;

import java.io.Serializable;

/**
 * Created with IntelliJ IDEA.
 * User: CM
 * Date: 27.10.12
 * Time: 23:57
 * To change this template use File | Settings | File Templates.
 */
public abstract class ArtCollection extends Identified implements Serializable{
    private String name;
    private String[] arts = new String[ArtSize.values().length];

    public ArtCollection(Object id, boolean local){
        super(local,id);
    }

    public ArtCollection(boolean local){
        super(local);
    }

    public String getArtUrl(ArtSize size){
        int index = size.ordinal();
        return arts[index];
    }

    private boolean setArtUrlAndCheckForUpdates(ArtSize size, String url){
        int index = size.ordinal();
        String oldUrl = arts[index];
        boolean result;

        if(oldUrl != null) {
            result = !oldUrl.equals(url);
        } else {
            result = url != null;
        }

        arts[index] = url;
        return result;
    }

    public final void setArtUrl(ArtSize size, String url){
        if(!setArtUrlAndCheckForUpdates(size, url)){
            return;
        }

        onUpdateArt();
    }

    public final void setUrlForAllArts(String url){
        boolean shouldBeUpdated = false;

        for(int i = 0; i < arts.length; i++){
            ArtSize size = ArtSize.values()[i];
            if(setArtUrlAndCheckForUpdates(size,url)){
                shouldBeUpdated = true;
            }
        }

        if(shouldBeUpdated){
            onUpdateArt();
        }
    }

    public void cloneArtUrlsFrom(ArtCollection artCollection){
        arts = artCollection.arts.clone();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }

    protected void onUpdateArt(){

    }
}
